<?php
namespace Company\Controller;

use Think\Controller;

class GoodsController extends Controller
{

    public function GoodAdd()
    {
        /**
         * 添加产品
         */
        $data["name"] = $_POST["name"];
        $data["price"] = $_POST["price"];
        $data["des"] = $_POST["des"];
        $data["img"] = "";

        if ($_FILES["file"]["error"]) {
            echo "文件上传失败";
            $this->error();
        } else {
            if (file_exists("Public/upload/" . $_FILES["file"]["name"])) {
                $this->error('文件名已存在','getGoods');
            } else {
                $data["img"] = $_FILES["file"]["name"];
                $img = "Public/upload/" . $_FILES["file"]["name"];
                move_uploaded_file($_FILES["file"]["tmp_name"], $img);
                $good = D("product");
                $re = $good->add($data);

                if ($re) {
                    $this->success('产品添加成功！','getGoods');
                } else {
                    $this->error('产品添加失败','getGoods');
                }
            }

        }
    }

    /**
     * 产品列表
     */
    public function getGoods()
    {
        //  获取数据库产品信息
        $product = D('product');
        $list = $product->order('id desc')->select();
        $this->assign("list", $list);
        $this->display();

    }

    public function upGoods()
    {
        //  获取产品列表显示在编辑页
        $id = $_GET["id"];
        $d = D("product");
        $list = $d->where("id='$id'")->select();
        $this->assign("list", $list);
        $this->display();
    }


    public function deleteGoods()
    {

        //  获取产品列表id
        $id = $_GET["id"];
        //  根据id删除该行表格数据
        $delete = D("product");

        $url = $delete->where('id=%d',$id)->select();
        $urlName = "Public/upload/".$url[0]["img"];

        if (!unlink($urlName)){
            unlink($urlName);
        }
        $de = $delete->execute("delete from ds_product where id='$id'");
        if (!empty($de)) {
            $this->success('删除成功', '/Goods/getGoods');
        } else {
            $this->error('删除失败', '/Goods/getGoods');
        }

    }

    public function addGoods()
    {
        //  更改产品
        $id=$_GET["id"];
        $name=$_POST["name"];
        $price=$_POST["price"];
        $des=$_POST["des"];

        $product=D("product");

        if($name!=null and $price!=null and $des!=null){

            $data["name"]=$name;
            $data["price"]=$price;
            $data["des"]=$des;
            if($_FILES['file']['error']!=4){
                if ($_FILES["file"]["error"]) {
                    $this->error("文件上传失败");
                }
                else {
                    if (file_exists("Public/upload/" . $_FILES["file"]["name"])) {
                        $this->error('文件名已存在','getGoods');
                    }
                    else {
                        $data["img"] = $_FILES["file"]["name"];
                        $img = "Public/upload/" . $_FILES["file"]["name"];
                        move_uploaded_file($_FILES["file"]["tmp_name"], $img);
                    }
                }
            }

            $re=$product->where("id='$id'")->save($data);
            if(!empty($re)){
                $this->success('更改成功','getGoods');
            }
            else{
                $this->error('更改失败','getGoods');
            }
        }else{
            echo "内容不能为空！";
        }

    }

}




